/*
#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int n, five = 0, ten = 0;
int bills[N];
bool check (int bills[], int n)
{
	for (int i = 0; i < n; i ++)
	{
	if (bills[i] == 5)
	five ++;
	else if (bills[i] == 10)
	{
		if (five != 0)
	    {
		    ten ++;
			five --;
		}
		else
		return false;
	}
	else
	{
		if (five != 0 && ten != 0)
		{
			ten --;
			five --;
		}
		else if (five >= 3)
		{
			five -=3;
		}
		else
		return false;
	}
	return true;		
	}
}
int main()
{
    scanf ("%d",&n);
    for (int i = 0; i < n; i ++)
       scanf ("%d", &bills[i]);
	bool a = check(bills, n);
	return 0;
}
*/






























